package com.example.google_map_test;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.EditText;


public class ADD_2 extends Activity{

	OnClickListener	backClick	= null;
	OnClickListener listener_update = null;
	OnClickListener listener_delete = null;

	Button button_update;
	Button button_delete;

	DBConnection helper;
	public int id_this;

	
	public interface UserSchema {
			String TABLE_NAME = "Users";          //Table Name
			String ID = "_id";                    //ID
			String USER_NAME = "user_name";       //User Name
			String ADDRESS = "address";           //Address
			String TELEPHONE = "telephone";       //Phone Number
			String MAIL_ADDRESS = "mail_address"; //Mail Address
			String PHOTO_ADDRESS = "photo_address";
		}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_2);

        
        backClick = new OnClickListener()
        {
        	public void onClick(View v) 
			{
				Intent intent3=new Intent();
				intent3.setClass(ADD_2.this,MainActivity.class);
				startActivity(intent3);
			}
        };
        
        Button backBtn=(Button)findViewById(R.id.Button05);
        backBtn.setOnClickListener(backClick);
       
        

            
        final EditText mEditText01 = (EditText)findViewById(R.id.EditText01);
        final EditText mEditText02=  (EditText)findViewById(R.id.EditText02);
		final EditText mEditText03 = (EditText)findViewById(R.id.EditText03);
		final EditText mEditText04 = (EditText)findViewById(R.id.EditText04);
		final EditText mEditText05 = (EditText)findViewById(R.id.EditText05);
		
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();	//取得Bundle
		

		
		//建立資料庫PhoneBookDB和表單Table:Users
		helper = new DBConnection(this);
        final SQLiteDatabase db = helper.getWritableDatabase();	
        //取得所有資料的USER_NAME，安置在list[]上
        Cursor c = db.query(UserSchema.TABLE_NAME, new String[] {UserSchema.USER_NAME}, null, null, null, null, null);
		c.moveToFirst();
		CharSequence[] list = new CharSequence[c.getCount()];
		for (int i = 0; i < list.length; i++) {
				list[i] = c.getString(0);
				c.moveToNext();
		}
		c.close();
		
		
		Cursor c1 = db.query("Users", 
				new String[] {UserSchema.ID,UserSchema.USER_NAME, UserSchema.TELEPHONE, UserSchema.ADDRESS,UserSchema.MAIL_ADDRESS,UserSchema.PHOTO_ADDRESS} 
				,"_id='"+bundle.getString("id")+"'"
				, null, null, null, null);
		c1.moveToFirst();
		id_this = Integer.parseInt(c1.getString(0));
		String user_name_this = c1.getString(1);
		String telephone_this = c1.getString(2);
		String address_this = c1.getString(3);
		String mail_address_this = c1.getString(4);
		String photo_address_this = c1.getString(5);
		c1.close();
		mEditText01.setText(user_name_this);
		mEditText02.setText(telephone_this);
		mEditText03.setText(address_this);
		mEditText04.setText(mail_address_this);
		mEditText05.setText(photo_address_this);
			
		
		File imgFile = new  File(photo_address_this);
		if(imgFile.exists()){

		    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

		    ImageView myImage = (ImageView) findViewById(R.id.img);
		    myImage.setImageBitmap(myBitmap);

		}

		
		
		//按下[Update]按鈕時，更新一筆資料
		listener_update = new OnClickListener() {
			public void onClick(View v) {
				ContentValues values = new ContentValues();
				values.put(UserSchema.USER_NAME, mEditText01.getText().toString());
				values.put(UserSchema.TELEPHONE, mEditText02.getText().toString());
				values.put(UserSchema.ADDRESS, mEditText03.getText().toString());
				values.put(UserSchema.MAIL_ADDRESS, mEditText04.getText().toString());
				//values.put(UserSchema.PHOTO_ADDRESS, mEditText05.getText().toString());
				String where = UserSchema.ID + " = " + id_this;
				SQLiteDatabase db = helper.getWritableDatabase();
				db.update(UserSchema.TABLE_NAME, values, where ,null);
				db.close();
				onCreate(savedInstanceState);
			}
		};
		//按下[Delete]按鈕時，刪除一筆資料
		listener_delete = new OnClickListener() {
			public void onClick(View v) {
				String where = UserSchema.ID + " = " + id_this;
				SQLiteDatabase db = helper.getWritableDatabase();
				db.delete(UserSchema.TABLE_NAME, where ,null);
				db.close();
				Intent intent3=new Intent();
				intent3.setClass(ADD_2.this,MainActivity.class);
				startActivity(intent3);
			}
		};

		//設定BUTTON0i,i=1,2,3,4的OnClickListener

		button_update = (Button)findViewById(R.id.Button02);
		button_update.setOnClickListener(listener_update);
		button_delete = (Button)findViewById(R.id.Button03);
		button_delete.setOnClickListener(listener_delete);	     
    }

    
    
    public static class DBConnection extends SQLiteOpenHelper {
		private static final String DATABASE_NAME = "PhoneBookDB";
		private static final int DATABASE_VERSION = 1;
		private DBConnection(Context ctx) {
			super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
		}
		public void onCreate(SQLiteDatabase db) {
			String sql = "CREATE TABLE " + UserSchema.TABLE_NAME + " (" 
			+ UserSchema.ID  + " INTEGER primary key autoincrement, " 
			+ UserSchema.USER_NAME + " text not null, " 
			+ UserSchema.TELEPHONE + " text not null, " 
			+ UserSchema.ADDRESS + " text not null, "
			+ UserSchema.MAIL_ADDRESS + " text not null, "
			+ UserSchema.PHOTO_ADDRESS + " text not null "+ ");";
			//Log.i("haiyang:createDB=", sql);
			db.execSQL(sql);	
		}
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub	
		}
	} 
}

